package com.ll.agent.ASMStudy.delay;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

/**
 * @description:
 * @author: LiuLei
 * @create: 2024-07-19
 **/
public class Delay extends AdviceAdapter {
        protected Delay(MethodVisitor mv, int access, String name, String descriptor) {
            super(ASM7, mv, access, name, descriptor);
        }
        @Override
        protected void onMethodEnter() {
            // 在方法开始处插入10秒延迟
            mv.visitLdcInsn(10000L); // 加载常量10000L
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V", false); // 调用Thread.sleep(long)

            // try-catch块：捕获并处理InterruptedException
            mv.visitLabel(new org.objectweb.asm.Label());
            mv.visitTryCatchBlock(
                    new org.objectweb.asm.Label(), // try开始
                    new org.objectweb.asm.Label(), // try结束
                    new org.objectweb.asm.Label(), // catch开始
                    "java/lang/InterruptedException" // 捕获的异常类型
            );
            mv.visitInsn(RETURN); // 捕获到异常后返回

            super.onMethodEnter(); // 在方法其余部分执行正常代码
        }
}
